package main

import (
	"fmt"
	"log"
	"net/http"
	"runtime"
	"strconv"
	"time"
)

func main() {
	static := func() {
		for {
			time.Sleep(time.Second)
			total := runtime.NumGoroutine()
			fmt.Println("total:", total)
		}

	}
	go static()
	http.HandleFunc("/sum", sumHandle)
	log.Fatal(http.ListenAndServe(":8000", nil))

}
func sumHandle(w http.ResponseWriter, r *http.Request) {
	s := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
	ch1, ch2 := make(chan int), make(chan int)
	go sum(s[:len(s)/2], ch1)
	go sum(s[len(s)/2:], ch2)
	x, y := <-ch1, <-ch2
	fmt.Fprintf(w, strconv.Itoa(x+y))
}
func sum(data []int, c chan int) {
	comm := 0
	for _, v := range data {
		comm += v
	}
	c <- comm
}
